#!/usr/bin/env python
# encoding: utf-8
'''
@author: zhaowenpeng
@contact: winston@peipeiyun.com
@software: garner
@file: __init__.py.py
@time: 2020/6/2 3:43 下午
@desc:
'''

import inspect
import time
from functools import wraps
from typing import Any, List
from core.my_log import logger

def fullname(o):
    module = inspect.getmodule(o)
    return f"{module.__name__}.{o.__qualname__}"

def timer(func: Any):
    """Timing decorator that sends a timing metric."""

    @wraps(func)
    def wrapper(*args, **kwargs):
        start = time.perf_counter()
        result = func(*args, **kwargs)
        elapsed_time = int(time.perf_counter()-start)
        logger.info(f"function:{fullname(func)} elapsed.time:{elapsed_time}")
        return result
    return wrapper

def apply(decorator: Any, exclude: List[str] = None):

    if not exclude:
        exclude = []
    def decorate(cls):
        for attr in cls.__dict__:
            if callable(getattr(cls, attr)) and attr not in exclude:
                setattr(cls, attr, decorator(getattr(cls, attr)))
        return cls
    return decorate
